package com.cloud.thread;

import com.cloud.commons.constants.ConsumeConstant;
import com.cloud.commons.utils.DateUtils;
import com.cloud.dao.assetsdetail.AssetsDetailDao;
import com.cloud.dao.wxuser.WxUserDao;
import com.cloud.dao.wxusersubscribe.WxUserSubscribeDao;
import com.cloud.model.entity.assetsdetail.AssetsDetail;
import com.cloud.model.entity.wxuser.WxUser;
import com.cloud.model.entity.wxusersubscribe.WxUserSubscribe;
import com.cloud.model.view.bookkeeping.BookkeepingView;
import com.cloud.service.wxtemplatemessage.WxTemplateMessageService;
import com.mysql.cj.util.TestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;

/**
 * 子线程 - 负责发送模板消息
 */
@Component
public class WxTemplateMessageThread extends Thread {

    private Logger LOG = LoggerFactory.getLogger(WxTemplateMessageThread.class);

    private BookkeepingView bookkeepingView;

    public static WxTemplateMessageThread wxTemplateMessageThread;

    @Autowired
    private WxUserDao wxUserDao;

    @Autowired
    private AssetsDetailDao assetsDetailDao;

    @Autowired
    private WxUserSubscribeDao wxUserSubscribeDao;

    @Autowired
    private WxTemplateMessageService wxTemplateMessageService;

    /**
     * 普通类调用bean 方法初始化
     */
    @PostConstruct
    public void init() {
        wxTemplateMessageThread = this;
        wxTemplateMessageThread.wxUserDao = this.wxUserDao;
        wxTemplateMessageThread.assetsDetailDao = this.assetsDetailDao;
        wxTemplateMessageThread.wxUserSubscribeDao = this.wxUserSubscribeDao;
        wxTemplateMessageThread.wxTemplateMessageService = this.wxTemplateMessageService;

    }




    public WxTemplateMessageThread(BookkeepingView bookkeepingView) {
        this.bookkeepingView = bookkeepingView;
    }

    public WxTemplateMessageThread() {}
    public void run() {
        LOG.info("我这边调用了多线程发送模板消息接口");
        Long userId = bookkeepingView.getUserId();
        WxUser wxUser = wxTemplateMessageThread.wxUserDao.findById(userId).orElseThrow(() -> new RuntimeException("获取不到用户信息 id是：" + userId));
        // 获取当月时间点
        Long monthFirst = DateUtils.getCurrentMonthFirstDay(bookkeepingView.getConsumeTime());
        AssetsDetail assetsDetail = wxTemplateMessageThread.assetsDetailDao.findByEnabledAndUserIdAndNowFirstMonth(ConsumeConstant.ENABLED,userId,monthFirst);
        if (ObjectUtils.isEmpty(assetsDetail)  || wxUser.getSendMessage().equals(ConsumeConstant.DISABLED)) {
            return;
        }
        WxUserSubscribe bookkeepingSuccess = wxTemplateMessageThread.wxUserSubscribeDao.findByUserIdAndSubscribeId(userId,ConsumeConstant.BOOKKEEPING_SUCCESS);
        // 发送消费成功模板消息
        if (!ObjectUtils.isEmpty(bookkeepingSuccess) && bookkeepingSuccess.getType().intValue() == ConsumeConstant.SUBSCRIBE_SUCCESS) {
            wxTemplateMessageThread.wxTemplateMessageService.bookkeepingSuccessMessage(bookkeepingView,wxUser);
        }
        // 发送记账预算模板消息
        WxUserSubscribe accountingBudget = wxTemplateMessageThread.wxUserSubscribeDao.findByUserIdAndSubscribeId(userId,ConsumeConstant.ACCOUNTING_BUDGET);
        // 是否超过本身百分之80
        Boolean isBoolean = wxUser.getMonthBudget().intValue() - assetsDetail.getMonthTotalExpenditure().intValue() < wxUser.getMonthBudget() * 0.2;
        if (!ObjectUtils.isEmpty(accountingBudget)  && isBoolean && accountingBudget.getType().intValue() == ConsumeConstant.SUBSCRIBE_SUCCESS) {
            wxTemplateMessageThread.wxTemplateMessageService.accountingBudgetMessage(bookkeepingView,wxUser,assetsDetail.getMonthTotalExpenditure());
        }
        LOG.info("子线程 - 发送模板消息请求结束");
    }
}
